<?php
class FacebookCatalogsController extends FacebookAppController {

	var $name = 'FacebookCatalogs';
	var $helpers = array('Html', 'Form');
	
	// Model
	var $uses = array('FacebookCatalogsController',FACEBOOK_DEFAULT_PRODUCT);
	
	/** 
	 * Name: beforeFilter 
	 * Desc: Make sure users are logged into facebook 
	 */  
	function beforeFilter() {  
		$this->user = $this->facebook->require_login();  
	} 

	/**
	 * Show the form to invite friends to join this app
	 */
	function index() {
		Configure::write('debug', '0');
		
		$this->set('selected_menu', "invites");
	}
	
	function details() {
		$this->set('user', $this->user);
		
		$model_name = $this->params['pass'][0];
		
		$object_id = $this->params['pass'][1];		
		if(class_exists($model_name)) {
			$this->set('product_model_name', $model_name);
			
			$product = new $model_name;
			$instance = $product->findById($object_id);
			$this->set('product', $instance);
			
			// Unique ID for using Facebook comments engine
			$comments_id = $model_name.'_'.$instance[$model_name]['id'];
			$this->set('comments_id', $comments_id);
			// URL to return to after a comment is posted
			$comments_return_url = FACEBOOK_CANVAS_URL.'facebook_catalogs/details/'.$model_name.'/'.$instance[$model_name]['id'];
			$this->set('comments_return_url', $comments_return_url);
			
			$this->set('product_plural', Inflector::humanize(Inflector::underscore(Inflector::pluralize($model_name))));
			$this->set('product_singular', Inflector::humanize(Inflector::underscore(Inflector::singularize($model_name))));
			
		}
	}
	
	function sent() {
		$conditions = array('fields' => array('DISTINCT FacebookAction.model_id',
											  'FacebookAction.model_name'),
							'conditions' => array('actor'=>$this->user),
							'order' => 'FacebookAction.t DESC',
							'limit' => 100,
							'recursive' => 0);
		$products = $this->FacebookAction->find('all', $conditions);
		foreach($products as &$p) {
			$model = new $p['FacebookAction']['model_name'];
			$instance = $model->findById($p['FacebookAction']['model_id']);
			$p['product'] = $instance;
		}
		$this->set('products',$products);
		
		// Menu Tab
		$this->set('selected_menu', "sent");
	}
	
	function received() {
		$conditions = array('fields' => array('DISTINCT FacebookAction.model_id',
											  'FacebookAction.model_name'),
							'conditions' => array('target'=>$this->user),
							'order' => 'FacebookAction.t DESC',
							'limit' => 100,
							'recursive' => 0);
		$products = $this->FacebookAction->find('all', $conditions);
		foreach($products as &$p) {
			$model = new $p['FacebookAction']['model_name'];
			$instance = $model->findById($p['FacebookAction']['model_id']);
			$p['product'] = $instance;
		}
		$this->set('products',$products);
		
		// Menu Tab
		$this->set('selected_menu', "received");
	}

	
}
?>
